<template>
  <div class="index-container">
    <header class="header">
      <h1>首页</h1>
      <div class="user-info">
        <span>{{ userInfo.username }}</span>
        <button @click="handleLogout">退出登录</button>
      </div>
    </header>

    <main class="main-content">
      <!-- 这里是首页内容 -->
      <h2>欢迎访问首页</h2>
    </main>
  </div>
</template>

<script>
import authService from "@/asstes/api/authService";

export default {
  name: "Index",
  data() {
    return {
      userInfo: {
        username: "",
      },
    };
  },
  created() {
    // 获取用户信息
    const userInfo = JSON.parse(localStorage.getItem("userInfo") || "{}");
    this.userInfo = userInfo;

    console.log("Index page created", {
      userInfo: this.userInfo,
      token: localStorage.getItem("token"),
    });
  },
  methods: {
    async handleLogout() {
      try {
        await authService.logout();
        this.$router.push("/login");
      } catch (error) {
        console.error("退出登录失败:", error);
        this.$router.push("/login");
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.index-container {
  min-height: 100vh;
  background-color: #f5f5f5;

  .header {
    background-color: #fff;
    padding: 20px;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
    display: flex;
    justify-content: space-between;
    align-items: center;

    h1 {
      margin: 0;
      font-size: 24px;
      color: #333;
    }

    .user-info {
      display: flex;
      align-items: center;
      gap: 20px;

      span {
        font-size: 16px;
        color: #666;
      }

      button {
        padding: 8px 16px;
        border: none;
        border-radius: 4px;
        background-color: #ff4d4f;
        color: #fff;
        cursor: pointer;
        transition: background-color 0.3s;

        &:hover {
          background-color: #ff7875;
        }
      }
    }
  }

  .main-content {
    padding: 20px;
    max-width: 1200px;
    margin: 0 auto;

    h2 {
      margin-bottom: 20px;
      color: #333;
    }
  }
}
</style>
